import 'package:enterprise_v6_app/app/components/custom_app_bar.dart';
import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/special_operation/custom_title_chart_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/widget_extension.dart';
import 'package:enterprise_v6_app/app/data/entities/task/base_risk_object_entity.dart';
import 'package:enterprise_v6_app/app/modules/task/task_all/risk_control/risk_control_approve/risk_control_approve_controller.dart';

///安全风险辨识管理审签
class RiskControlApprovePage extends StatelessWidget {
  RiskControlApprovePage({super.key});

  final controller = Get.put(RiskControlApproveController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            key: controller.riskRightScaffoldKey,
            backgroundColor: AppColors.bg_color_grey,
            appBar: CustomAppBar(
              titleText: '安全风险辨识管理审签',
            ),
            body: Container(
              color: AppColors.bg_color_grey,
              padding: EdgeInsets.only(top: 12.w, right: 14.w, left: 14.w),
              child: Column(
                children: [
                  Expanded(
                      child: CustomEasyRefreshList<BaseRiskObjectEntity>(
                    key: controller.globalKey,
                    enableLoad: false,
                    dataLoad: controller.getObjectAllByCompanyCode,
                    itemBuilder: (item, index) {
                      return _itemWidget(item);
                    },
                  )),
                ],
              ),
            ),
          );
        });
  }

  Widget _rowData(BaseRiskObjectEntity item, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Container(
      padding: EdgeInsets.symmetric(horizontal: 16.w, vertical: 20.w),
      child: Column(
        children: [
          _getApprovalButton(item),
        ],
      ),
    );
  }

  /// 获取审批按钮
  Widget _getApprovalButton(BaseRiskObjectEntity item) {
    return Row(
      mainAxisAlignment: MainAxisAlignment.spaceBetween,
      children: [
        Text(
          '操作:',
          style: TextStyle(
            fontSize: 13.sp,
            height: 1.2,
            fontWeight: FontWeight.w400,
            color: const Color(0xffA5A6B5),
          ),
        ),
        Container(
          padding: EdgeInsets.fromLTRB(10.w, 6.w, 6.w, 6.w),
          decoration: BoxDecoration(
            color: const Color(0xFFE5E7FF),
            borderRadius: BorderRadius.circular(4.w),
          ),
          child: Row(
            children: [
              Text(
                '审批',
                style: TextStyle(
                  fontSize: 13.sp,
                  height: 1.2,
                  fontWeight: FontWeight.w400,
                  color: const Color(0xff5C6DFF),
                ),
              ),
              AppGaps.hGap2,
              SvgPicture.asset(
                AppImages.icon_right_arrow,
              ),
            ],
          ),
        )
      ],
    );
  }

  /// 根据状态获取状态图标
  /// 审批状态 0：待发起 1：待审批 2：审批中 3：通过 4：驳回
  static Widget getStateView(int state) {
    String? stateText;
    Color? color;
    switch (state) {
      case 0:
        stateText = '待发起';
        color = const Color(0xFF0ED0D0);
        break;
      case 1:
        stateText = '待审批';
        color = const Color(0xFFFFA500);
        break;
      case 2:
        stateText = '审批中';
        color = const Color(0xFFB9B907);
        break;
      case 3:
        stateText = '通过';
        color = const Color(0xFF0FCE0F);
        break;
      case 4:
        stateText = '已驳回';
        color = const Color(0xFFBB0CBB);
        break;
    }
    return Container(
      alignment: Alignment.center,
      height: 18.w,
      width: 52.w,
      decoration: BoxDecoration(
        color: color ?? Colors.white,
        borderRadius: BorderRadius.only(
          topRight: Radius.circular(20.w),
          topLeft: Radius.circular(20.w),
          bottomRight: Radius.circular(20.w),
        ),
      ),
      child: Text(
        stateText ?? '',
        style: TextStyle(
          fontSize: 10.sp,
          fontWeight: FontWeight.w400,
          color: Colors.white,
        ),
      ),
    );
  }

  Widget _itemWidget(BaseRiskObjectEntity item) {
    return CustomTitleChartPage(
      title: item.riskObjectName ?? '',
      titleRightWidget: getStateView(item.checkType ?? -1),
      contentPadding: EdgeInsets.symmetric(horizontal: 0.w, vertical: 0.w),
      child: _rowData(item, '', alignment: Alignment.centerLeft).clickOnce(() {
        controller.itemDetail(item);
      }),
    );
  }
}
